﻿using DevExpress.Office.Utils;
using DevExpress.Pdf.ContentGeneration.Interop;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace GluePathModule
{
    [XmlInclude(typeof(GluePathItemArc)),XmlInclude(typeof(GluePathItemCircle))
        , XmlInclude(typeof(GluePathItemLine)), XmlInclude(typeof(GluePathItemPoint)), XmlInclude(typeof(GluePathItemSPLine))]
    public class GluePathItemBase
    {
        public string Name;
        [Browsable(false)]
        public bool Select
        {
            get;
            set;

        }
        [Browsable(false)]
        public string Remark
        {
            get;
            set;

        }

        [Browsable(false)]
        public int iAdjustStep
        {
            get;
            set;
        }

        [Description("是否屏蔽"), Category("通用设定"), DefaultValue(false)]
        public bool Disable
        {
            get;
            set;

        }
        [Category("开胶延时"), DefaultValue(0.0)]
        public double 开胶后延时时间
        {
            get;
            set;
        }

        
        [Category("个性化设定"), DefaultValue(10.0)]
        public bool 使用单独涂胶速度
        {
            get;
            set;
        }
        [Category("个性化设定"), DefaultValue(10.0)]
        public double 单独涂胶速度
        {
            get;
            set;
        }
        [Category("个性化设定"), DefaultValue(false)]
        public bool 空动作路径
        {
            get;
            set;
        }
        public GluePathItemBase()
        {

        }
        virtual public void DrawItem(DrawerControl drawer,DrawMode drawMode= DrawMode.UnitIsSelected)
        {
            return;
        }
      
        virtual public bool IsOnSelect(double dPosX, double dPosY, double dJudageDis)
        {
            return false;
        }
        
        virtual public bool GetLastPoint(out double dPosX, out double dPosY, out double dPosZ, out double dPosU)
        {
            dPosX = 0.0;
            dPosY = 0.0;
            dPosZ = 0.0;
            dPosU = 0.0;
            return false;
        }
        virtual public bool GetFirstPoint(out double dPosX, out double dPosY, out double dPosZ, out double dPosU)
        {
            dPosX = 0.0;
            dPosY = 0.0;
            dPosZ = 0.0;
            dPosU = 0.0;
            return false;
        }
        virtual public GluePathItemBase CopyItem()
        {
            GluePathItemBase itemNew = (GluePathItemBase)MemberwiseClone();
            return itemNew;
        }
        virtual public GluePathItemBase Paste()
        {

            return this;
        }
        virtual public void PathUnitMouseDown(double dMouseX, double dMouseY, double dScandle)
        {

        }
        virtual public void PathUnitMouseMove(double dMouseX, double dMouseY)
        {
        }
        virtual public void PathUnitMouseUp(double dMouseX, double dMouseY)
        {
        }
        virtual public void ChangedValue(string strPropertyLabel, Object oldValue)
        {
        }
        virtual public void OffSetItem(double dOffSetX, double dOffSetY)
        {

        }

       

        virtual public void OffSetPos(double dOffSetX, double dOffSetY, double dOffSetZ)
        {

        }
        virtual public void SetZPos(double dSetPosZ)
        {

        }
        virtual public void SetUPos(double dSetPosU)
        {

        }
        virtual public void UpdateZheight(double dZHeight)
        {

        }
        virtual public void WriteToDxf(netDxf.DxfDocument dxfDocument)
        {

        }
        virtual public void GetItemMinMaxXY(out double dMinX, out double dMinY, out double dMaxX, out double dMaxY)
        {
            dMinX = 0.0;
            dMinY = 0.0;
            dMaxX = 0.0;
            dMaxY = 0.0;
        }
        virtual public void UpdateEndPos(double dNewEndX, double dNewEndY)
        {

        }
        virtual public void UpdateStartPos(double dNewStartX, double dNewStartY)
        {

        }
        virtual public void PushItem(List<MotionItemBase> motionItems,GluePathGroup gluePathGroup,GluePathUnit gluePathUnit, int indexId,double dOffset)
        {
            
        }
        public PointData WhereAtLocal(PointData position, double dLocalStandardX,double dLocalStandardY,double dLocalStandardU)
        {
            PointData tempPosTool = new PointData(position);
            PointData tempPos = new PointData(position);
            double dTempXTest = tempPosTool.X - dLocalStandardX;
            double dTempYTest = tempPosTool.Y - dLocalStandardY;
            double dSinAngle = Math.Sin(dLocalStandardU * Math.PI / 180.0);
            double dCosAngle = Math.Cos(dLocalStandardU * Math.PI / 180.0);
            tempPos.X = dTempXTest * dCosAngle + dTempYTest * dSinAngle;
            tempPos.Y = dTempYTest * dCosAngle - dTempXTest * dSinAngle;
            tempPos.U = tempPosTool.U - dLocalStandardU;
            return tempPos;
        }
        public PointData TranLocalToMotor(PointData posLocal,double dLocalCurretX, double dLocalCurrentY, double dLocalCurrentU)
        {
            //LocalToCurrent
            PointData tempPos = new PointData(posLocal);
           
            
            double dSinAngle = Math.Sin(dLocalCurrentU * Math.PI / 180.0);
            double dCosAngle = Math.Cos(dLocalCurrentU * Math.PI / 180.0);
            tempPos.X = dLocalCurretX + posLocal.X * dCosAngle - posLocal.Y * dSinAngle;
            tempPos.Y = dLocalCurrentY + posLocal.X * dSinAngle + posLocal.Y * dCosAngle;
                
            tempPos.U = posLocal.U+ dLocalCurrentU;
            

            return tempPos;
        }
        public PointData TrantToolToTool0(PointData posToolNozzle, double ToolX, double ToolY)
        {
            //LocalToCurrent
            PointData tempPos = new PointData(posToolNozzle);

            double dToolAngle = Math.Atan2(ToolY, ToolX)*360.0/Math.PI;
            double dR = Math.Sqrt(ToolX * ToolX + ToolY * ToolY);
            double dSinAngle = Math.Sin((dToolAngle+posToolNozzle.U) * Math.PI / 180.0);
            double dCosAngle = Math.Cos((dToolAngle + posToolNozzle.U) * Math.PI / 180.0);
            double dOffSetX = dR * dCosAngle;
            double dOffSetY = dR * dSinAngle;

            tempPos.X = tempPos.X - dOffSetX;
            tempPos.Y = tempPos.X - dOffSetY;
            return tempPos;
        }
        protected void AddToolOffSet(GluePathUnit pathUnit, PointData pos)
        {
            Matrix matrixToTool = new Matrix();
            matrixToTool.Rotate((float)pos.U);
            matrixToTool.Translate((float)(pathUnit.CurrentToolX), (float)(pathUnit.CurrentToolY));
            PointF[] pfTool = new PointF[1];
            pfTool[0].X = (float)(0.0);
            pfTool[0].Y = (float)(0.0);
            matrixToTool.TransformPoints(pfTool);
            pos.X = pos.X - pfTool[0].X;
            pos.Y = pos.Y - pfTool[0].Y;
        }
    }
}
